//
//  DemoScrollImageViewViewController.m
//  DemoScrollImageView
//
//  Created by duc nguyen van on 8/11/11.
//  Copyright 2011 STU. All rights reserved.
//

#import "DemoScrollImageViewViewController.h"

@implementation DemoScrollImageViewViewController

@synthesize crollView, pageControl, crollViewDown;

#define IMAGE_WIDTH   728
#define IMAGE_HEIGHT  732

#define IMAGE_WIDTH_DOWN	200
#define IMAGE_HEIGHT_DOWN	200

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

#pragma mark -
#pragma mark scrollDelegate

-(void)setResourceOfScrollViewDown:(UIScrollView*)scrollView with:(NSMutableArray*)resources{
	int i = 0;
	for (NSString *image in resources) {
		UIImage *ima = [UIImage imageNamed:image];
		UIImageView *imageView = [[UIImageView alloc] initWithImage:ima];
		[imageView.layer setBorderWidth:5.0];
		
		imageView.contentMode = UIViewContentModeScaleToFill;
		imageView.clipsToBounds = YES;
		
		imageView.frame = CGRectMake( IMAGE_WIDTH_DOWN * i++, 0, IMAGE_WIDTH_DOWN, IMAGE_HEIGHT_DOWN);
		[scrollView addSubview:imageView];
		[imageView release];
	}
	scrollView.contentSize = CGSizeMake(IMAGE_WIDTH_DOWN * i, IMAGE_HEIGHT_DOWN);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
	CGFloat pageWidth = scrollView.frame.size.width;
	int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
	pageControl.currentPage = page;
	
	//set resource
	switch (page) {
		case 0:
			[self setResourceOfScrollViewDown:crollViewDown with:arrImageOne];
			break;
		case 1:
			[self setResourceOfScrollViewDown:crollViewDown with:arrImageTwo];
			break;
		case 2:
			[self setResourceOfScrollViewDown:crollViewDown with:arrImageThree];
			break;
		default:
			break;
	}
	
}

#pragma mark -

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	//create Image one, two, three
	arrImageOne = [[NSMutableArray alloc] initWithObjects:@"Hinh02.jpg",@"Hinh03.jpg",@"Hinh04.jpg",@"Hinh05.jpg",nil];
	arrImageTwo = [[NSMutableArray alloc] initWithObjects:@"Hinh17.jpg",@"Hinh18.jpg",@"Hinh19.jpg",@"Hinh20.jpg",nil];
	arrImageThree = [[NSMutableArray alloc] initWithObjects:@"Hinhdep01.jpg",@"Hinhdep02.jpg",@"Hinhdep03.jpg",@"Hinhdep04.jpg",nil];
	
	//create first image of projects
	int index = 0;
	arrProject = [[NSMutableArray alloc] initWithObjects:[arrImageOne objectAtIndex:index],
				  [arrImageTwo objectAtIndex:index],[arrImageThree objectAtIndex:index],nil];
	
	//fill image into scrollView
	int i = 0;
	for (NSString *image in arrProject) {
		UIImage *ima = [UIImage imageNamed:[arrProject objectAtIndex:i]];
		UIImageView *imageView = [[UIImageView alloc] initWithImage:ima];
		
		imageView.contentMode = UIViewContentModeScaleToFill;
		imageView.clipsToBounds = YES;
		
		imageView.frame = CGRectMake( IMAGE_WIDTH * i++, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
		
		[crollView addSubview:imageView];
		[imageView release];
	}
	crollView.contentSize = CGSizeMake(IMAGE_WIDTH*i, IMAGE_HEIGHT);
	
	crollView.delegate = self;	
	pageControl.currentPage = 0;
	pageControl.numberOfPages = [arrProject count];
	
	//fill image into scrollViewDown
	[self setResourceOfScrollViewDown:crollViewDown with:arrImageOne];
}



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
